package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.tianxin.workbao.workBao.Activity.TransContractActivity;
import com.tianxin.workbao.workBao.Activity.TransPriceActivity;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.TransContract;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.dataProvide.TransContractProvide;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.SPUtils;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.utils.common.ToastUtils;
import com.tianxin.workbao.workBao.view.ExpandView;

import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class TransContractAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<TransContract> mTransContracts;
    private Context mContext;

    public TransContractAdapter(List<TransContract> list, Context context) {
        this.mTransContracts = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (mTransContracts.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.trans_contract_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            mPosition = position;
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(mTransContracts.get(position));
            viewHolder.SetViewListener(position);
        }
    }

    @Override
    public int getItemCount() {
        if (mTransContracts.size() > 0) {
            return mTransContracts.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView arrawIv;
        TextView numberTv;
        TextView timeTv;
        TextView sendCompanyTv;
        TextView buyCompanyTv;
        TextView coalNameTv;
        ExpandView expandView;
        TextView transPayInfo;
        TextView transCompanyTv;
        TextView sendAaaressTv;
        TextView receiptAaaressTv;
        TextView statusLabelTv;

        Button deleteBtn;
        Button oauthBtn;
        Button finishBtn;
        Button toVoidBtn;
        Button changePriceBtn;

        View mItemView;

        public ViewHolder(View itemView) {
            super(itemView);
            mItemView = itemView;
            arrawIv = (ImageView) itemView.findViewById(R.id.arrow);
            numberTv = (TextView) itemView.findViewById(R.id.number);
            timeTv = (TextView) itemView.findViewById(R.id.time);
            sendCompanyTv = (TextView) itemView.findViewById(R.id.send_company);
            buyCompanyTv = (TextView) itemView.findViewById(R.id.buy_company);
            coalNameTv = (TextView) itemView.findViewById(R.id.coal_name);
            expandView = (ExpandView) itemView.findViewById(R.id.expandView);
            transPayInfo = (TextView) itemView.findViewById(R.id.trans_pay_info);
            transCompanyTv = (TextView) itemView.findViewById(R.id.trans_company);
            sendAaaressTv = (TextView) itemView.findViewById(R.id.send_address);
            receiptAaaressTv = (TextView) itemView.findViewById(R.id.receipt_address);
            deleteBtn = (Button) itemView.findViewById(R.id.delete_btn);
            oauthBtn = (Button) itemView.findViewById(R.id.oauth_btn);
            finishBtn = (Button) itemView.findViewById(R.id.finish_btn);
            toVoidBtn = (Button) itemView.findViewById(R.id.to_void_btn);
            changePriceBtn = (Button) itemView.findViewById(R.id.change_price_btn);
            statusLabelTv = (TextView) itemView.findViewById(R.id.status_label);
        }

        public void BindViewData(TransContract contract) {
            numberTv.setText(contract.getTctnum());
            sendCompanyTv.setText(contract.getSendcompanyname());
            sendAaaressTv.setText(contract.getSendaddress());
            buyCompanyTv.setText(contract.getReceivecompanyname());
            receiptAaaressTv.setText(contract.getReciveaddress());
            coalNameTv.setText(contract.getCoaltypename());
            coalNameTv.setText(contract.getCoaltypename());
            transCompanyTv.setText(contract.getTranscompany());
            String info = contract.getTranspaytype() == 1 ? "收货净重 " : "发货净重 ";
            info += contract.getLosstype() == 1 ? "不计路耗 " : (contract.getLosstype() == 2 ? "减合理路耗 " : "超出全扣");
            info += contract.getIsaddtax() == 1 ? "含税 " : "不含税 ";
            info += contract.getIsblurred() == 1 ? "区分内外价" : "不区分内外价";
            transPayInfo.setText(info);
            switch (contract.getStatus()) {
                case 0:
                    statusLabelTv.setText("未审");
                    timeTv.setText(TimeUtils.parseDateTime(contract.getSigntime()));
                    deleteBtn.setVisibility(View.VISIBLE);
                    oauthBtn.setVisibility(View.VISIBLE);
                    finishBtn.setVisibility(View.GONE);
                    toVoidBtn.setVisibility(View.GONE);
                    changePriceBtn.setVisibility(View.GONE);
                    break;
                case 1:
                    statusLabelTv.setText("已审");
                    timeTv.setText(TimeUtils.parseDateTime(contract.getTransstarttime()));
                    deleteBtn.setVisibility(View.GONE);
                    oauthBtn.setVisibility(View.GONE);
                    finishBtn.setVisibility(View.VISIBLE);
                    toVoidBtn.setVisibility(View.VISIBLE);
                    changePriceBtn.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    statusLabelTv.setText("完毕");
                    timeTv.setText(TimeUtils.parseDateTime(contract.getTransendtime()));
                    deleteBtn.setVisibility(View.GONE);
                    oauthBtn.setVisibility(View.GONE);
                    finishBtn.setVisibility(View.GONE);
                    toVoidBtn.setVisibility(View.GONE);
                    changePriceBtn.setVisibility(View.GONE);
                    break;
                case 3:
                    statusLabelTv.setText("作废");
                    timeTv.setText(TimeUtils.parseDateTime(contract.getTransendtime()));
                    deleteBtn.setVisibility(View.GONE);
                    oauthBtn.setVisibility(View.GONE);
                    finishBtn.setVisibility(View.GONE);
                    toVoidBtn.setVisibility(View.GONE);
                    changePriceBtn.setVisibility(View.GONE);
                    break;
            }
        }

        public void SetViewListener(final int position) {
            View.OnClickListener listener = new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    TransContract contract;
                    switch (view.getId()) {
                        case R.id.arrow:
                            if (expandView.isExpand()) {
                                expandView.collapse();
                                arrawIv.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp);
                            } else {
                                expandView.expand();
                                arrawIv.setImageResource(R.drawable.ic_arrow_drop_up_black_24dp);
                            }
                            break;
                        case R.id.delete_btn:
                            getTransContractProvide().delete((int) mTransContracts.get(position).getId());
                            expandView.setVisibility(View.GONE);
                            break;
                        case R.id.oauth_btn:
                            contract = mTransContracts.get(position);
                            contract.setAudituserid((int) SPUtils.getPrefLong(Constant.CURRENT_USER_ID, 0));
                            contract.setAuditusername(SPUtils.getPrefString(Constant.CURRENT_USER_NAME, "no"));
                            contract.setStatus(TransContract.STATUS_ZXZ);
                            expandView.setVisibility(View.GONE);
                            getTransContractProvide().option(contract);
                            break;
                        case R.id.finish_btn:
                            contract = mTransContracts.get(position);
                            contract.setStatus(TransContract.STATUS_ZXWB);
                            expandView.setVisibility(View.GONE);
                            getTransContractProvide().option(contract);
                            break;
                        case R.id.to_void_btn:
                            contract = mTransContracts.get(position);
                            contract.setStatus(TransContract.STATUS_ZF);
                            expandView.setVisibility(View.GONE);
                            getTransContractProvide().option(contract);
                            break;
                        case R.id.change_price_btn:
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("contract", mTransContracts.get(position));
                            Intent intent = new Intent(mContext, TransPriceActivity.class);
                            intent.putExtras(bundle);
                            mContext.startActivity(intent);
                            break;
                    }
                }
            };
            arrawIv.setOnClickListener(listener);
            deleteBtn.setOnClickListener(listener);
            oauthBtn.setOnClickListener(listener);
            finishBtn.setOnClickListener(listener);
            toVoidBtn.setOnClickListener(listener);
            changePriceBtn.setOnClickListener(listener);
        }
    }

    private int mPosition;
    private UpdateListViewListener listener = new UpdateListViewListener() {
        @Override
        public void onRefreshData(Object result) {

        }

        @Override
        public void onLoadMoreData(Object result) {

        }

        @Override
        public void showMessage(String msg) {
            if (msg == "ok") {
                mTransContracts.remove(mPosition);
                notifyDataSetChanged();
                ToastUtils.showLong("操作成功");
            } else if (msg.contains("failure")) {
                ToastUtils.showLong("操作失败！\n " + msg.substring(msg.indexOf("failure") + 1, msg.length()));
            } else {
                ToastUtils.showLong(msg);
            }
        }

        @Override
        public void showProgress() {
            TransContractActivity activity = (TransContractActivity) mContext;
            activity.getSweetAlertDialog(activity).showDialog("", 3000);
        }

        @Override
        public void hideProgress() {
            TransContractActivity activity = (TransContractActivity) mContext;
            activity.getSweetAlertDialog(activity).dismiss();
        }
    };
    private TransContractProvide provide;

    public TransContractProvide getTransContractProvide() {
        if (provide == null) {
            provide = new TransContractProvide(mContext, listener);
        }
        return provide;
    }
}
